package com.hand.wfive.loginAndVip.ui

import android.content.Intent
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.hand.wfive.R
import com.hand.wfive.activity.PrivacyActivity
import com.hand.wfive.loginAndVip.LoginConfig
import com.hand.wfive.loginAndVip.UserManager
import com.hand.wfive.util.click.NoFastClickUtils
import com.qmuiteam.qmui.widget.QMUITopBarLayout
import kotlinx.android.synthetic.main.login_activity_login_password.*

class LoginPasswordActivity : LoginBaseActivity() {

    override fun getTopBar(): QMUITopBarLayout = topBar

    override fun loginSuccess() {

    }

    override fun getContentViewId() = R.layout.login_activity_login_password

    override fun init() {
        initLogin()
        tvWelcome.text = "欢迎登录${getString(R.string.app_name)}"
    }

    fun loginPasswordBtnClick(v: View) {
        if (NoFastClickUtils.isFastClick()) return
        when (v) {
            login_huawei -> /*华为账号登录*/ huaweiLogin()
            login_wechat -> /*微信一键登录*/ wechatLogin()
            login_password_op -> /*密码显示 or 隐藏*/ {
                login_password_op.isSelected = !login_password_op.isSelected
                if (login_password_op.isSelected) {
                    login_password_op.setImageResource(R.mipmap.login_password_show)
                    login_password.transformationMethod =
                        HideReturnsTransformationMethod.getInstance()
                } else {
                    login_password_op.setImageResource(R.mipmap.login_password_hide)
                    login_password.transformationMethod =
                        PasswordTransformationMethod.getInstance()
                }
                login_password.setSelection(login_password.length())
            }
//            login_turn_code -> /*验证码登录*/ finish()
            login_turn_register -> /*注册*/ {
                val intent = Intent(mContext, RegisterActivity::class.java)
                intent.putExtra(LoginConfig.paramsIsBuy, mIsBuy)
                mOtherLogin.launch(intent)
            }
            login -> /*登录*/ loginPassword()
            login_privacy_policy -> /*隐私政策*/ {
                PrivacyActivity.showRule(mContext, PrivacyActivity.ShowPolicy)
            }
            login_user_agreement -> /*用户协议*/ {
                PrivacyActivity.showRule(mContext, PrivacyActivity.ShowUserAgreement)
            }
            login_policy -> /* 勾选隐私政策和用户协议 */ {
                login_policy_agree.isSelected = !login_policy_agree.isSelected
                if (login_policy_agree.isSelected) {
                    login_policy_agree.setImageResource(R.mipmap.login_checkbox_sel)
                } else {
                    login_policy_agree.setImageResource(R.mipmap.login_checkbox_nor)
                }
            }
        }
    }

    private fun loginPassword() {
        val account = login_account.text.toString()
        if (account.isEmpty()) {
            showNormalTip(topBar, "请输入账号")
            return
        }
        val password = login_password.text.toString()
        if (password.isEmpty()) {
            showNormalTip(topBar, "请输入密码")
            return
        }
        if (!login_policy_agree.isSelected) {
            showNormalTip(topBar, "请阅读并同意隐私政策和用户协议")
            return
        }

        showLoading("正在登录")
        login(account, password , UserManager.LoginType_normal)
    }

    /*  private fun login(name: String, password: String) {
          val passwordMd5 = MD5Util.getPasswordMd5(password)
          RxHttp.postForm(ApiConfig.login)
              .add("appid", LoginConfig.AppId)
              .add("username", name)
              .add("pwd", passwordMd5)
              .add("loginType", UserManager.LoginType_normal)
              .add("appname", getString(R.string.app_name))
              .add("packageName", App.getContext().packageName)
              .asClass(ApiModel::class.java)
              .to(RxLife.toMain(this))
              .subscribe({ apiModel ->
                  hideLoading()
                  if (apiModel.code == ApiConfig.SUCCESSCODE) {
                      toast("登录成功")
                      val user = apiModel.obj
                      user.password = passwordMd5
                      UserManager.getInstance().saveUser(user)
                      if (mIsBuy && !UserManager.getInstance().isVip) {
                          startActivity<VipCenterActivity>()
                      }
                      setResult(RESULT_OK)
                      finish()
                  } else {
                      if (TextUtils.isEmpty(apiModel.msg)) {
                          showNormalTip(topBar, "网络异常，请重试！")
                      } else {
                          showNormalTip(topBar, apiModel.msg)
                      }
                  }
              }, {
                  hideLoading()
                  showNormalTip(topBar, "登录失败")
              })
      }*/
}
